<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.OcrUitl" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.weaver.general.BaseBean" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.util.HashMap" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    String ids = Util.null2String( request.getParameter( "ids" ) );//文件ID
    JSONObject jsonData = new JSONObject();
    boolean flag = true;
    String msg = "";
    String sql = "select fplx,fpzw,fpdm,fphm,fprq,bhsje,checkCode,sellerRegisterNum from uf_fpxx where id in (" + ids + ")";
    rs.executeQuery( sql );
    while (rs.next()) {
        String fpzw = Util.null2String( rs.getString( "fpzw" ) );
        String fplx = Util.null2String( rs.getString( "fplx" ) );
        String fpdm = Util.null2String( rs.getString( "fpdm" ) );
        String fphm = Util.null2String( rs.getString( "fphm" ) );
        String fprq = Util.null2String( rs.getString( "fprq" ) );
        String bhsje = Util.null2String( rs.getString( "bhsje" ) );
        String checkCode = Util.null2String( rs.getString( "checkCode" ) );
        String sellerRegisterNum = Util.null2String( rs.getString( "sellerRegisterNum" ) );//预留电话
        bb.writeLog( String.join( "-", fplx, fpdm, fphm, fprq, bhsje, checkCode, sellerRegisterNum ) );
        if("0".equals( fplx ) ||"1".equals( fplx ) ||"11".equals( fplx )||"14".equals( fplx )||"15".equals( fplx )){
            if ("1".equals( fpzw ) || "2".equals( fpzw )) {
                Map<String,Object> resultMap = invoicerecertify( fplx, fpdm, fphm, fprq, bhsje, checkCode, sellerRegisterNum );
                 flag = (boolean) resultMap.get("flag");
                 msg = (String) resultMap.get("msg");
            }  else if ("0".equals( fpzw )) {
                flag = false;
                msg = "系统提示：发票已经验真通过";
            }else {
                flag = false;
                msg = "系统提示：只有[认证不通过]的发票才能重新验真";
            }
        }else {
            flag = false;
            msg = "系统提示：此发票不能验真";
        }
    }
    jsonData.put( "flag", flag );
    jsonData.put( "msg", msg );
    out.print( jsonData.toJSONString() );
    bb.writeLog( "getInvoiceVerified_Ajax=" + jsonData.toJSONString() );
%>
<%!
    //发票认证
    public static Map<String,Object> invoicerecertify(String invoiceType, String invoiceCode, String invoiceNum, String invoiceDate, String totalAmount, String checkCode, String purchaserRegisterNum) {
        BaseBean baseBean = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>(  );
        String YZ_API_KEY = baseBean.getPropValue( "langchao", "YZ_API_KEY" );
        String yz_url = baseBean.getPropValue( "langchao", "yz_url" );
        RecordSet rs = new RecordSet();
        String recertifyCode = invoiceCode + invoiceNum;
        JSONObject params = new JSONObject();
        String newinvoiceType = OcrUitl.getYZTicketType( invoiceType );
        params.put( "InvoiceType", newinvoiceType );//发票类型
        params.put( "InvoiceCode", invoiceCode );//发票代码
        params.put( "InvoiceNumber", invoiceNum );//发票号码
        params.put( "DateOfInvoice", invoiceDate );//开票日期
        params.put( "Amount", totalAmount );//未税金额
        String subCheckCode = checkCode.length() >= 6 ? checkCode.substring( checkCode.length() - 6 ) : checkCode;
        params.put( "CheckCode", subCheckCode );//校验码后六位
        params.put( "TaxpayerIDNo", purchaserRegisterNum );//税号
        baseBean.writeLog("发票验真参数："+params.toJSONString());
        JSONObject returnJson = OcrUitl.getInvoiceValidate( yz_url, YZ_API_KEY, params );
        JSONObject ReturnInfo = JSONObject.parseObject( returnJson.getString( "ReturnInfo" ) );
        String Code = ReturnInfo.getString( "Code" );
        String Message = ReturnInfo.getString( "Message" );
        baseBean.writeLog( "验真接口返回----Code=" + Code + ", Message=" + Message );
        JSONObject Data = JSONObject.parseObject( returnJson.getString( "Data" ) );
        if("01".equals(newinvoiceType)){
            boolean dzkyz = OcrUitl.getInvoiceStoreQuery(invoiceNum,invoiceCode,purchaserRegisterNum);
            if ("0".equals( Code )) {//验真通过
                if(dzkyz){
                    rs.executeUpdate( "update uf_fpxx set fpzw='0' where concat(fpdm,fphm)='" + recertifyCode + "'" );
                    OcrUitl.insertXhqdInfo( Data );
                    resultMap.put("flag",true);
                    resultMap.put("msg","发票验真通过");
                }else {
                    rs.executeUpdate( "update uf_fpxx set fpzw='1' where concat(fpdm,fphm)='" + recertifyCode + "'" );
                    resultMap.put("flag",false);
                    resultMap.put("msg","底账库不存在此发票");
                }
            } else {//验真不通过
                rs.executeUpdate( "update uf_fpxx set fpzw='1' where concat(fpdm,fphm)='" + recertifyCode + "'" );
                resultMap.put("flag",false);
                resultMap.put("msg",Message);
            }
        }else {
            if ("0".equals( Code )) {//验真通过
                rs.executeUpdate( "update uf_fpxx set fpzw='0' where concat(fpdm,fphm)='" + recertifyCode + "'" );
                OcrUitl.insertXhqdInfo( Data );
                resultMap.put("flag",true);
                resultMap.put("msg","发票验真通过");
            } else {//验真不通过
                rs.executeUpdate( "update uf_fpxx set fpzw='1' where concat(fpdm,fphm)='" + recertifyCode + "'" );
                resultMap.put("flag",false);
                resultMap.put("msg",Message);
            }
        }
        return resultMap;
    }
%>



